package com.demo.servlet;

import com.demo.dao.UsersDAO;
import com.demo.entity.Users;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/deluser")
public class UsersDelServlet extends HttpServlet {

    private UsersDAO usersDAO = new UsersDAO();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        doPost(req, res);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        // 获取参数
        String id = req.getParameter("id");
        String type = req.getParameter("type");

        // 执行删除 / 锁定操作
        if ("lock".equals(type)) {
            // 执行锁定
            Users user = new Users();
            user.setId(Integer.parseInt(id));
            user.setUserStatus(1);
            usersDAO.updateUsers(user);
        } else if ("del".equals(type)) {
            // 执行删除
            usersDAO.delUser(Integer.parseInt(id));
        } else if ("unlock".equals(type)) {
            // 执行解锁
            Users user = new Users();
            user.setId(Integer.parseInt(id));
            user.setUserStatus(0);
            usersDAO.updateUsers(user);
        }

        // 跳转到首页
        res.sendRedirect("/index");
    }
}
